package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 添加新的床位
     * @param bedDto 床位数据传输对象
     */
    @Override
    public void addBed(BedDto bedDto) {
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //toBean可以改用copyProperites方法
        //直接将bedDto这个对象中的属性值赋值给Bed对象
        // 前提是两者的属性名,属性类型要完全一致
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bed.setBedStatus(0);
        try {
            bedMapper.addBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public void updateBed(BedDto bedDto) {
//        BedVo bedVo = getBedById(bedDto.getId());
//        if (ObjectUtil.isNotNull(bedVo)) {
//        这里的修改跟新增对象的逻辑一样,同样可以使用BeanUtils.copyProperites方法将对象中的属性值赋值给bed对象
//        上面两行代码只为了优化,注释掉也可以正常运行
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bed.setUpdateTime(LocalDateTime.now());
        bed.setUpdateBy(1L);
        bedMapper.updateBed(bed);
//    }
    }

    @Override
    //通过id查询数据
    public BedVo getBedById(Long id) {
        return BeanUtil.toBean(bedMapper.getBedById(id), BedVo.class);
    }

    @Override
    public void deleteBedById(Long id) {
        bedMapper.deleteBedById(id);
    }


}

